package com.mall.db.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.UmsMenuMapper;
import com.mall.domain.UmsMenu;
import com.mall.interface1.api.UmsMenuApi;
import com.mall.vo.UmsMenuSonVo;
import com.mall.vo.UmsMenuVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UmsMenuAPiImpl implements UmsMenuApi {
   @Autowired
    private UmsMenuMapper umsMenuMapper;

    @Override
    public List<UmsMenu> queryId(List<Long> menuIds) {
       return umsMenuMapper.selectBatchIds(menuIds);
    }

    @Override
    public void save(UmsMenu umsMenu) {
        umsMenuMapper.insert(umsMenu);
    }

    @Override
    public List<Long> querySon(Integer id) {
        LambdaQueryWrapper<UmsMenu> wq = new LambdaQueryWrapper<>();
        wq.in(UmsMenu::getParentId,id);
        List<UmsMenu> menus = umsMenuMapper.selectList(wq);
        List<Long> ids = CollUtil.getFieldValues(menus, "id", Long.class);
        return ids;
    }

    @Override
    public void delete(List<Long> ids) {
        umsMenuMapper.deleteBatchIds(ids);
    }

    @Override
    public void deleteById(Integer id) {
        umsMenuMapper.deleteById(id);
    }

    @Override
    public IPage<UmsMenu> list(Integer pageNum, Integer pageSize, Integer parentId) {
        IPage<UmsMenu> list=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<UmsMenu> qw = new LambdaQueryWrapper<>();
        qw.in(UmsMenu::getParentId,parentId);
        IPage<UmsMenu> lists = umsMenuMapper.selectPage(list, qw);
        List<UmsMenu> records = lists.getRecords();
        List<Long> ids = CollUtil.getFieldValues(records, "id", Long.class);
        List<UmsMenu> menus = umsMenuMapper.selectBatchIds(ids);
        lists.setRecords(menus);
        return lists;
    }

    @Override
    public List<UmsMenuVo> listTree() {
        LambdaQueryWrapper<UmsMenu> qw = new LambdaQueryWrapper<>();
        qw.eq(UmsMenu::getParentId,0L);
        List<UmsMenu> menusOne = umsMenuMapper.selectList(qw);
        Map<Long, List<UmsMenu>> map = findSon(menusOne);
        //
        //
        List<UmsMenuVo> vois = new ArrayList<>();
        for (UmsMenu umsMenu : menusOne) {
            UmsMenuVo vo = new UmsMenuVo();
            List<UmsMenu> menus = map.get(umsMenu.getId());
            vo.setChildren(menus);
            BeanUtils.copyProperties(umsMenu,vo);
            vois.add(vo);
        }
        return vois;
    }

    @Override
    public void update(UmsMenu umsMenu) {
        umsMenuMapper.updateById(umsMenu);
    }

    @Override
    public UmsMenu selectId(Integer id) {
        return umsMenuMapper.selectById(id);
    }

    @Override
    public List<UmsMenu> findByIds(List<Long> menuIds) {
        return umsMenuMapper.selectBatchIds(menuIds);
    }

    private Map<Long,List<UmsMenu>> findSon(List<UmsMenu> menusOne) {
        List<Long> ids = CollUtil.getFieldValues(menusOne, "id", Long.class);

//        List<UmsMenuSonVo> list=new ArrayList<>();
//        Map<Long,List<UmsMenuSonVo>> map=new HashMap<>();
        Map<Long,List<UmsMenu>> resultMap=new HashMap<>();
        for (Long id : ids) {
            LambdaQueryWrapper<UmsMenu> wq = new LambdaQueryWrapper<>();
            wq.in(UmsMenu::getParentId,id);
            List<UmsMenu> menus = umsMenuMapper.selectList(wq);
//            UmsMenuSonVo vo = new UmsMenuSonVo();
//            BeanUtils.copyProperties(menus,vo);
//            list.add(vo);
//            map=new HashMap<>();
//            map.put(id,list);
            resultMap.put(id,menus);
        }
        return  resultMap;

    }

}
